package com.ddc.study.feature.query;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.ddc.study.R;
import com.ddc.study.databinding.ItemQueryHistoryBinding;
import com.ddc.study.entity.History;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.paging.PagedListAdapter;
import androidx.recyclerview.widget.DiffUtil;

/**
 * Created on 2019/11/16.
 *
 * @author lshun
 * @version 1.0
 */
public class QueryHistoryAdapter extends PagedListAdapter<History, SimpleViewHolder> {
    private static final DiffUtil.ItemCallback<History> DIFF_CALLBACK = new DiffUtil.ItemCallback<History>() {
        @Override
        public boolean areItemsTheSame(@NonNull History oldItem, @NonNull History newItem) {
            return oldItem == newItem;
        }

        @Override
        public boolean areContentsTheSame(@NonNull History oldItem, @NonNull History newItem) {
            return oldItem.equals(newItem);
        }
    };

    public QueryHistoryAdapter() {
        super(DIFF_CALLBACK);
    }

    @NonNull
    @Override
    public SimpleViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_query_history, parent, false);
        DataBindingUtil.bind(itemView);
        return new SimpleViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull SimpleViewHolder holder, int position) {
        ItemQueryHistoryBinding binding = DataBindingUtil.getBinding(holder.itemView);
        if (binding == null) return;
        binding.setHistory(getItem(position));
    }
}
